<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 路线模型
 * 
 * @desc    description
 * @version 1.0.0 
 * @author  Jason
 * @date    2017年5月15日 下午9:43:08
 */
class Mod_touroute extends MY_Model
{
	use models\common\MultipleLanguage;

	protected $table_mapper = [
			'master' => 'touroute',
			'period' => 'touroute_period',
	];
	
	protected $allowed_fields = [
			'master' => [
					'merchant_id', 'name', 'cover', 'images', 'languages', 
					'country', 'city', 'street', 'address', 'latitude', 'longitude', 
					'product_name', 'product_inventory', 'currency', 
					'mileage', 'duration', 'maximum', 'periods', 'summary', 'detail', 
					'business_hour', 'departure_time', 'departure_point', 
					'cost_inclusion', 'cost_exclusion', 'cancellation_policy', 
					'voucher_information', 'participation_notice', 
					'member_id', 'status', 
					'settle_currency', 'settle_amount', 'channel_currency', 'channel_amount', 
					'retail_cny', 'retail_usd', 'retail_eur',
			],
			'period' => ['touroute_id', 'date_day', 'begin', 'end', 'inventory', 'status'/*1为开放*/],
	];
	
	use models\common\BinaryBitBasedStatus;
	
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
		
		$this->multi_language_handler();
		
		$this->status_bit_config = [
				'master' => [
						// 启用禁用、是否审核
						'status_bit_length' => 3,
						'status_bit_positive_tags' => ['check_over', 'checked', 'enabled'],
						'status_bit_negative_tags' => ['check_doing', 'unchecked', 'disabled'],
				],
				'period' => [
						// 状态：0为关闭，1为开放
						'status_bit_length' => 1,
						'status_bit_positive_tags' => ['enabled'],
						'status_bit_negative_tags' => ['disabled'],
				],
		];
	}
	
	/**
	 * 插入前处理器
	 * 
	 * @desc  主要负责格式化一些字段
	 * @param array  $data
	 * @param string $mapping	表映射
	 *
	 * @return array
	 */
	public function before_create_data_handler($data, $mapping = 'master')
	{
		if ($mapping == 'master') {
			if (isset($data['languages'])) {
				$data['languages'] = is_array($data['languages'])
											? implode(',', $data['languages'])
											: $data['languages'];
			}
			if (isset($data['images'])) {
				$data['images'] = is_array($data['images'])
										? implode(',', $data['images'])
										: $data['images'];
			}
			// cover：封面从多图，但可指定
			if (empty($data['cover']) && isset($data['images'])) {
				# $data['cover'] = substr($data['images'], 0, strpos($data['images'], ','));
				# MARK 上面，images 中没有 "," 会出逻辑错误，而且不支持前导 ","!
				$haystack = trim($data['images'], ',');
				$length = strpos($haystack, ',') ?: strlen($haystack);
				$data['cover'] = substr($haystack, 0, $length);
			}
			$periods = @(array)json_decode($data['periods'], true);
			$data['periods'] = serialize($periods);
		}
		
		return $data;
	}
	
	/**
	 * 更新前处理器
	 * 
	 * @desc  主要负责格式化一些字段 & 过滤掉多语言字段，确保只在默认语言下更改主表的多语言字段
	 * @param number $id
	 * @param array  $data
	 * @param string $mapping	表映射
	 *
	 * @return array
	 */
	public function before_update_data_handler($id, $data, $mapping = 'master')
	{
		$data = $this->before_create_data_handler($data, $mapping);
		
		if ($mapping == 'master') {
			$language = $this->input->post('language');
			$default = $this->config->item('default_language');
			// 过滤掉多语言字段
			if ($language !== $default) {
				# $fields = $this->get_multi_language_info('touroute', $mapping);
				$fields = $this->get_multi_language_info('fields', $mapping);
				foreach ($fields as $field) {
					# Warning: Illegal offset type in isset or empty ??? $field mustn't be an array!
					# if (isset($data[$field])) { unset($data[$field]); }
					unset($data[$field]);
				}
			}
		}
		
		return $data;
	}
	
	public function get_as_poi_by_country_city($country, $city)
	{
		$conditions = [
				['select', 'id, merchant_id, cover, name, latitude, longitude'],
				['where', ["`status` & 1 > 0", null, false]],
		];
		empty($country) OR $conditions[] = ['where', "country='$country'"];
		empty($city) OR $conditions[] = ['where', "city='$city'"];
		$array = $this->select_instances_by_conditions($conditions);
		
		return $array;
	}
	
	public function get_ids_by_country_city($country, $city)
	{
		$conditions = [
				['select', 'id'],
				['where', ["`status` & 1 > 0", null, false]],
				
		];
		empty($country) OR $conditions[] = ['where', "country='$country'"];
		empty($city) OR $conditions[] = ['where', "city='$city'"];
		$array = $this->select_instances_by_conditions($conditions);
		$array = array_column($array, 'id');
		
		return $array;
	}

}
